<template>
  <div>
    <kendoForm :kendo-form-items="kendoFormItems" :kendo-form="kendoForm" :kendo-params-value="kendoParamsValue" />
  </div>
</template>

<script>
  import kendoForm from '@/Views/components/kendo-vue-component/kendoForm/index.vue'
  export default {
    name: 'TestCaseDetail',
    components: {
      kendoForm
    },
    data() {
      return {
        dialogName: this.$attrs.data.dialogName,
        kendoFormItems: [
          {
            name: 'reqNo',
            label: '需求名称',
            paramName: 'reqNo',
            readonly: true,
            controlType: 'multicolumncombobox',
            // change: this.devInChargeChange,
            dataSource: 'reqNoTaskDataSource',
            dataType: 'json',
            transportReadUrl: '/service/prjMng/reqmntTaskMng/findReqMntTaskList',
            dataMapping: {
              label: 'reqName',
              value: 'reqNo'
            },
            schema: {
              data: function(response) {
                if (typeof response.data === 'object') {
                  if (JSON.stringify(response.data) === '{}') {
                    return []
                  }
                }
                return response.data
              }
            },
            column: [
              {
                field: 'reqNo',
                title: '需求编号',
                width: 180
              },
              {
                field: 'reqName',
                title: '需求名称',
                width: 320
              }
            ],
            change: this.reqmntChange
          },
          {
            name: 'devInCharge',
            label: '开发负责人',
            paramName: 'devInCharge',
            required: true,
            viewDisabled: true,
            controlType: 'multiselect',
            // change: this.devInChargeChange,
            dataSource: 'PrjMemberDataSource',
            dataType: 'json',
            transportReadUrl: '/service/prjMng/prjMemberMng/findPrjMemberList',
            dataMapping: {
              label: 'memberName',
              value: 'memberNo'
            },
            autoBind: true,
            remoteData: true,
            schema: {
              data: function(response) {
                if (typeof response.data === 'object') {
                  if (JSON.stringify(response.data) === '{}') {
                    return []
                  }
                }
                return response.data
              }
            }
          },
          {
            name: 'reqInCharge',
            label: '需求负责人',
            paramName: 'reqInCharge',
            required: true,
            editDisabled: true,
            viewDisabled: true,
            controlType: 'input'
          },
          {
            name: 'reqTaskExecutor',
            label: '需求任务执行人',
            paramName: 'reqTaskExecutor',
            required: true,
            viewDisabled: true,
            controlType: 'input'
          },
          {
            name: 'testCaseGategory',
            label: '测试分类',
            paramName: 'testCaseGategory',
            required: true,
            disabled: true,
            viewDisabled: true,
            controlType: 'dropDownList',
            // change: this.devInChargeChange,
            dataSource: 'PrjMemberDataSource',
            dataType: 'json',
            transportReadUrl: '/service/prjMng/tccMng/findReqmntTccList',
            dataMapping: {
              label: 'testCaseGategoryNm',
              value: 'testCaseGategoryNo'
            },
            autoBind: true,
            remoteData: true,
            schema: {
              data: function(response) {
                if (typeof response.data === 'object') {
                  if (JSON.stringify(response.data) === '{}') {
                    return []
                  }
                }
                return response.data
              }
            },
            parameterMap: this.parameterMap
          },
          {
            name: 'testCaseName',
            label: '测试案例名称',
            paramName: 'testCaseName',
            required: true,
            editDisabled: true,
            controlType: 'input'
          },
          {
            name: 'testStartDt',
            label: '测试开始日期',
            paramName: 'testStartDt',
            required: true,
            editDisabled: true,
            controlType: 'datePicker',
            format: 'yyyy-MM-dd',
            viewDisabled: true
          },
          {
            name: 'testEndDt',
            label: '测试结束日期',
            paramName: 'testEndDt',
            readonly: true,
            editDisabled: true,
            controlType: 'datePicker',
            format: 'yyyy-MM-dd',
            viewDisabled: true
          },
          {
            label: '测试进度',
            name: 'caseTestScheduling',
            paramName: 'caseTestScheduling',
            required: true,
            viewDisabled: true,
            controlType: 'numeric',
            format: 'p2',
            min: 0,
            max: 1,
            step: 0.01,
            change: this.caseTestSchChange,
            spin: this.caseTestSchChange
          },
          {
            ref: 'WHETHER_CMPL_CD',
            remoteData: true,
            cascade: true,
            name: 'whetherCmpl',
            label: '是否完成',
            paramName: 'whetherCmpl',
            optionLabel: '--请选择--',
            controlType: 'dropDownList',
            filter: 'contains',
            dataType: 'json',
            transportReadUrl: '/service/sysMng/dict/findDictByTypeWithForm',
            required: true,
            autoBind: true,
            editDisabled: true,
            viewDisabled: true,
            schema: {
              data: function(response) {
                return response.data
              }
            },
            dataMapping: {
              label: 'dictValNm',
              value: 'dictVal'
            },
            parameterMap: function(options, operation) {
              if (operation !== 'read' && options.models) {
                // eslint-disable-next-line no-undef
                return { models: kendo.stringify(options.models) }
              } else {
                // eslint-disable-next-line no-undef
                return kendo.stringify({ dict: 'WHETHER_CMPL_CD' })
              }
            }
          },
          {
            ref: 'testProbDesc',
            genre: 'special',
            name: 'testProbDesc',
            label: '测试问题描述',
            paramName: 'testProbDesc',
            required: true,
            editDisabled: true,
            controlType: 'editor',
            imageBrowser: {
              transport: {
                read: {
                  url: '/service/imageBrowser/read',
                  type: 'POST'
                },
                destroy: {
                  url: '/service/imageBrowser/destroy',
                  type: 'POST'
                },
                create: {
                  url: '/service/imageBrowser/create',
                  type: 'POST'
                },
                thumbnailUrl: '/service/imagePreview/thumbnail',
                uploadUrl: '/service/imageBrowser/upload',
                imageUrl: '/service/imagePreview/image?path={0}'
              },
              schema: {
                data: function(response) {
                  return response.data
                },
                model: {
                  id: 'id',
                  fields: {
                    name: 'materialName',
                    type: 'materialType',
                    size: 'materialSize'
                  }
                }
              }
            },
            tools: this.editorTools
          }
        ],
        kendoForm: {
          ref: 'ReqmntTestCaseForm',
          valid: 'valid',
          buttons: [
            {
              name: '保存', // 按钮内容
              class: 'k-primary', // 按钮样式
              icon: 'save',
              click: this.save
            },
            {
              name: '重置', // 按钮内容
              icon: 'reset-sm',
              click: this.reset
            }
          ],
          rules: {},
          message: {}
        },
        kendoParamsValue: {
          id: '',
          reqNo: '',
          testCaseGategory: '',
          testCaseNo: '',
          testCaseName: '',
          testStartDt: '',
          testEndDt: '',
          caseTestScheduling: '',
          whetherCmpl: '',
          testProbDesc: '',
          devInCharge: '',
          reqInCharge: '',
          reqTaskExecutor: '',
          createTime: '',
          createOrg: '',
          createRole: '',
          createUser: '',
          updateTime: '',
          updateOrg: '',
          updateRole: '',
          updateUser: '',
          hdlTime: '',
          hdlOrg: '',
          hdlRole: '',
          hdlUser: ''
        }
      }
    },
    mounted() {
      const { reqNo, code, name, nodeId, nodeType } = this.$attrs.data.param
      let parameterMap = {}
      if (nodeType === '1') {
        parameterMap = {
          reqNo
        }
      } else {
        parameterMap = {
          reqNo,
          code,
          name,
          id: nodeId
        }
      }
      this.getTestCaseInfo(parameterMap)
      // this.kendoParamsValue = Object.assign(this.kendoParamsValue, this.$attrs.data.param)
      const operatorType = this.$attrs.data.operatorType
      if (operatorType) {
        if (operatorType === 'edit') {
          this.kendoFormItems.forEach(item => {
            if (item.editDisabled) {
              item['readonly'] = true
            }
          })
          this.kendoForm = {
            ref: 'ReqmntTestCaseForm',
            valid: 'valid',
            buttons: [
              {
                name: '保存', // 按钮内容
                class: 'k-primary', // 按钮样式
                icon: 'save',
                click: this.save
              }
            ]
          }
        } else if (operatorType === 'view') {
          this.kendoFormItems.forEach(item => {
            if (item.viewDisabled) {
              item['disabled'] = true
            }
          })
        }
      }
    },
    methods: {
      parameterMap(options, operation) {
        const { reqNo } = this.$attrs.data.param
        if (operation !== 'read' && options.models) {
          // eslint-disable-next-line no-undef
          return { models: kendo.stringify(options.models) }
        } else {
          // eslint-disable-next-line no-undef
          return kendo.stringify({ reqNo: reqNo })
        }
      },
      save(paramVal, validator) {
        if (validator.validate()) {
          this.kendoFormItems.forEach(item => {
            if (Array.isArray(paramVal[item.paramName])) {
              this.$set(paramVal, item.paramName, paramVal[item.paramName].join(','))
            }
          })
          this.$post('/service/prjMng/testCaseMng/saveOrUpdateReqmntTestCase', paramVal).then(res => {
            if (res.code !== 200) {
              this.$KendoNotify({
                title: '错误信息:' + res.code,
                message: res.message
              }, 'error')
            } else {
              this.$KendoNotify({
                title: '提示信息:',
                message: res.message
              }, 'success')
              debugger
              this.$bus.emit('multildialog' + `${this.dialogName}`, true)
            }
          })
        }
      },
      reset(paramVal) {
        Object.keys(paramVal).forEach(key => {
          if (key !== 'reqNo') {
            if (Array.isArray(paramVal[key])) {
              this.kendoParamsValue[key] = []
            } else {
              this.kendoParamsValue[key] = ''
            }
          }
        })
      },
      getTestCaseInfo(data) {
        // 初始化需求问题跟踪编号
        const { code } = this.$attrs.data.param
        this.$post('/service/prjMng/testCaseMng/getReqmntTestCase', data).then(res => {
          if (res && res.code === 200) {
            this.$set(this.kendoParamsValue, 'testCaseGategory', code)
            this.kendoParamsValue = Object.assign(this.kendoParamsValue, res.data)
          } else {
            this.$KendoNotify({
              title: '错误信息:' + res.code,
              message: res.message
            }, 'error')
          }
        })
      },
      caseTestSchChange(e) {
        const val = e.sender.value()
        if (val === 1) {
          this.$set(this.kendoParamsValue, 'whetherCmpl', '01')
          this.$set(this.kendoParamsValue, 'testEndDt', this.dateFormat(new Date(), 'YYYY-MM-DD'))
        } else {
          this.$set(this.kendoParamsValue, 'whetherCmpl', '02')
          this.$set(this.kendoParamsValue, 'testEndDt', '')
        }
      }
    }
  }
</script>

<style>

</style>
